<?php


namespace App\Http\Controllers\Manage\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\System\Dictionary\Port\StoreRequest;
use App\Http\Requests\Manage\System\Dictionary\Port\UpdateRequest;
use App\Http\Searchs\Manage\System\Dictionary\PortSearch;
use App\Repositories\Port;

class PortController extends Controller
{
    /**
     * @var Port
     */
    private $port;

    /**
     * PortController constructor.
     * @param Port $port
     */
    public function __construct(Port $port)
    {
        $this->port = $port;
    }

    /**
     * @param PortSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(PortSearch $search){
        return $this->response(
            $this->port->paginate($search->toArray(), request()->input('per_page'))
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request){
        return $this->response(
            $this->port->create($request->only(['code', 'name']))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        return $this->response(
            $this->port->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id){
        return $this->response(
            $this->port->update($id, $request->only(['name']))
        );
    }
}
